#include "defaultcontenthandler.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>

#include "logger.h"
#include "helpers.h"



/*************************************************************************
 * Constructor 
 *************************************************************************/
defaultcontenthandler::defaultcontenthandler(){
	content_length=0;
	buffer=0;
	buffersize=0;
	content.data=0;
	content.datalength=0;
	content.currentdatasize=0;
	data_completed=0;
}


/*************************************************************************
 * Destructor
 *************************************************************************/
defaultcontenthandler::~defaultcontenthandler(){
	if(buffer)
		free(buffer);
	if(content.data)
		free(content.data);
	content.data=0;
}


/*************************************************************************
 * Called to handle data 
 *************************************************************************/
HANDLER_RESPONSE defaultcontenthandler::HandleData(char** buf, unsigned int *bufsize){
	if(*bufsize){
		content.data=(char*)realloc(content.data, sizeof(char)*(content.currentdatasize+*bufsize));
		memcpy(content.data+content.currentdatasize, *buf, *bufsize);
		content.currentdatasize+=*bufsize;
	}
	if(content.datalength==-1){
		data_completed=1;
		logd<<"DefaultHandler: No data length"<<lend;
		return HANDLER_FINISHED;
	}
	else if(content.datalength == content.currentdatasize){
		logd<<"DefaultHandler: Finished collecting data"<<lend;
		data_completed=1;
		return HANDLER_FINISHED;
	}
	else if(content.datalength > MAX_DEFCON_HANDLER_BUFFER_SIZE || content.currentdatasize >= MAX_DEFCON_HANDLER_BUFFER_SIZE ){
		logw<<"DefaultHandler: Buffer breached "<<MAX_DEFCON_HANDLER_BUFFER_SIZE<<" < "<<content.datalength<<lend;
		return HANDLER_FINISHED;
	}
	else{
		return HANDLER_MORE;
	}
}


/*************************************************************************
 * Called  to set the deader 
 *************************************************************************/
void defaultcontenthandler::HandleHeaders(HeaderHandlerInterface* headerd){
	headerh = headerd;
	const char *c;
	if((c=headerd->GetHeader("Content-Length")))
		content.datalength=atoi(c);
	else
		content.datalength=-1;
}


/*************************************************************************
 * Called (Should be only once) to get the handled data 
 *************************************************************************/
int defaultcontenthandler::GetHandledData(char** buf, unsigned int* bufsize){

	char* x;

	x = ((char*)realloc(*buf, sizeof(char)*content.currentdatasize));
	*buf=x;	
	memcpy(*buf, content.data, content.currentdatasize);
		
	*bufsize=content.currentdatasize;	

	//free(buffer);
	//buffer=0;
	//buffersize=0;

	if(content.data)
		free(content.data);
	content.data=0;

	if(data_completed)
		return HANDLER_FINISHED;
	else
		return HANDLER_COPOUT;
}


